//
//  selectGiftView.m
//  tcpoctest
//
//  Created by gump on 5/22/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "selectGiftView.h"

#import "GiftListDataSource.h"
#import "GiftInfoDataSource.h"

#import "sendGiftListLength.h"
#import "sendPersonReadCoin.h"

@implementation selectGiftView

-(id)init
{
    self = [super init];
    
    if (self != nil) {
        iGameState = VGS_NULL;
        iTimeCount = 0;
    }
    
    return self;
}

-(void)dealloc
{
    
}

#pragma mark -private fun-

-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];
}

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"礼物列表";
    
    mtable =[[UITableView alloc]initWithFrame:CGRectMake(0, TOPBAR_HIGHT, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 20)
                                        style:UITableViewStylePlain];
    mtable.backgroundColor=CELL_BG_COLOR;
    mtable.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    mtable.delegate =self;
    mtable.dataSource =self;
    [self.view insertSubview:mtable belowSubview:self.mtitle];
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
}

-(void)refreshView
{
    [mtable reloadData];
}

-(void)viewWillShow
{
    [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"正在获取礼物列表"
                                                      withView:self.view];
    
    iTimeCount = 0;
    iGameState = VGS_START;
    
    [self refreshView];
}

-(void)UICustomTick
{
    switch (iGameState) {
        case VGS_START:
        {
            if (iTimeCount < 200) {
                iTimeCount++;
            }
            else
            {
                iTimeCount = 0;
                iGameState = VGS_NULL;
                
                [[ViewManager sharedViewManager] dismissProgressHUD];
            }
        }
            break;
            
        default:
            break;
    }
}

-(void)SelectGiftRequestBakc
{
    [[ViewManager sharedViewManager] dismissProgressHUD];
    
    [mtable reloadData];
    
    iTimeCount = 0;
    iGameState = VGS_NULL;
}

#pragma mark - tableView DataSource -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0)
    {
        if ([DataControl shareControl].giftlistDS != nil) {
            
            return [[DataControl shareControl].giftlistDS GetShowArrCount];
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section=[indexPath section];
    NSUInteger row = [indexPath row];
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
		cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.imageView.image = nil;
    cell.textLabel.text = @"";
    
    switch (section)
    {
        case 0:
        {
            if ([DataControl shareControl].giftlistDS != nil &&
                row < [[DataControl shareControl].giftlistDS GetShowArrCount]) {
                
                cell.textLabel.text = [[DataControl shareControl].giftlistDS GetNameByRow:row];
                
                NSString *strpath = [[DataControl shareControl].giftlistDS searchPathByRow:row];
                
                if (strpath == nil || [strpath isEqualToString:@""]) {
                    cell.imageView.image = [UIImage imageNamed:@"gift_default.png"];
                }
                else
                {
                    cell.imageView.image = [UIImage imageWithContentsOfFile:strpath];
                }

            }
            
            
        }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            break;
        default:
            break;
    }
    
    return cell;
}

#pragma mark -table delegate-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch ([indexPath section]) {
        case 0:
        {
            if ([DataControl shareControl].OnlinePeoDS != nil)
            {
                int row = [indexPath row];
                if (row >=0 && row < [[DataControl shareControl].giftlistDS GetShowArrCount])
                {
                    int iid = [[DataControl shareControl].giftlistDS GetGiftIDByRow:row];
                    
                    [[DataControl shareControl].giftInfoDS SetShowArrByListID:iid];
                    
                   [[ViewManager sharedViewManager] showView:VI_GIFTLISTINFO
                                            withIsRemoveCurV:false
                                               withDirection:VD_RIGHT];
                    
                    [[netManager sharedNetManager].sendgiftLL sendGiftLL:iid];
                    
                    [[netManager sharedNetManager].sendPersonRC sendReadCoin];
                }
                
            }
        }
            break;
            
        default:
            break;
    }
}

@end
